<?php
/**
 * Created by PhpStorm.
 * User: xionggang
 * Date: 15-4-30
 * Time: 上午9:55
 */

class Article_edit extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("admin/article_model");
        $this->load->model("admin/type_model");
        $this->load->model("admin/tag_model");
    }


    /**
     * auto:xg
     * 添加文章
     */
    public function add(){
        $catid = $this->input->get('catid') ? $this->input->get('catid',true) : '';
        if(!empty($catid)){
            $res['typeid'] = $catid;
            //获取标签信息

            $this->data['tag_list'] = $this->tag_model->getlist();

            //获取栏目信息

            foreach($this->type_model->getList() as $v){
                $type_arr[$v['catid']] = $v['typename'];
            }
            $this->data['type_arr'] = $type_arr;
            $res['pubtime'] = date('Y-m-d H:i:s',time());
            $res['source'] = '5TGAME.COM';
            $res['writer'] = $_COOKIE['nickname'];
            $this->data['info'] = $res;
            $this->load->view('admin/article_edit', $this->data);
        }
    }

    /**
     * auto：xg
     * 编辑文章
     */
    public function edit(){
        $id = $this->input->get('id',true);
        if($id>0){
            //获取文章信息

            $res = $this->article_model->get_one_article($id);
            $res['pubtime'] = date('Y-m-d H:i:s',$res['pubtime']);
            //获取标签信息

            $this->data['tag_list'] = $this->tag_model->getlist();

            //获取栏目信息

            foreach($this->type_model->getList() as $v){
                $type_arr[$v['catid']] = $v['typename'];
            }
            $this->data['type_arr'] = $type_arr;
            $this->data['info'] = $res;
            $this->load->view('admin/article_edit', $this->data);
        }
    }

    /**
     * auto:xg
     * 保存文章
     */
    public function save(){
        if(isset($_POST) && !empty($_POST)){
            $id = $this->input->post('id') ? $this->input->post('id',true) : '';
            $post_date['title'] = $this->input->post('title') ? $this->input->post('title',true) : '';
            $post_date['subtitle'] = $this->input->post('subtitle') ? $this->input->post('subtitle',true) : '';
            $flag_arr = $this->input->post('flag') ? $this->input->post('flag',true) : '';
            $flag_arr && $post_date['flag'] = implode(',',$flag_arr);


            $post_date['tag']= $this->input->post('tag') ? $this->input->post('tag',true) : '';
            $post_date['typeid']= $this->input->post('typeid') ? $this->input->post('typeid',true) : '';
            $post_date['source']= $this->input->post('source') ? $this->input->post('source',true) : '';
            $post_date['writer']= $this->input->post('writer') ? $this->input->post('writer',true) : '';
            $post_date['flash_pic']= $this->input->post('flash_pic') ? $this->input->post('flash_pic',true) : '';
            $pubtime = $this->input->post('pubtime') ? $this->input->post('pubtime',true) : '';
            $post_date['pubtime']= strtotime($pubtime);
            $post_date['content']= $this->input->post('body') ? $this->input->post('body',true) : '';
            $post_date['keywords']= $this->input->post('keywords') ? $this->input->post('keywords',true) : '';
            $post_date['description']= $this->input->post('description') ? $this->input->post('description',true) : '';

            $save_flag = $this->article_model->save_article($id,$post_date);
            if ($save_flag == 1) {
                cpmessage("操作成功", "/index.php?d=admin&c=article_list");
            } else {
                cpmessage("操作失败", "/index.php?d=admin&c=article_list");
            }
        }
    }
    /**
     * auto:xg
     * 检查标签
     */
    public function chack_tag(){
        $tags = $this->input->post('tags') ? $this->input->post('tags',true) : '';

        $tag_arr = explode('，',$tags);
        $ktj_tag = '';
        foreach($tag_arr as $v){
            $tagname = '';
            $tagname = $this->data['info'] = $this->tag_model->get_tag_byname($v);
            if(empty($tagname)){
                $ktj_tag .= $v.'，';
            }
        }
        $ktj_tag = rtrim($ktj_tag,'，');
        echo $ktj_tag;exit;
    }
}